﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Model;

namespace GatewayInspect.Modules
{
    public partial class KQGLModule : BaseModule
    {
        private string selectedOrgCode = "";

        public KQGLModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            this.toolStripSeparator1.Visible = false;
            this.tsBtnImportExcel.Visible = false;
            this.tsBtnDownTemplate.Visible = false;
            this.tsBtnSave.Visible = false;
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();

            string strSQL = "select ID,ORGCODE,ORGNAME,ISSYSTEM,DECODE(ISSYSTEM,'1','是','否') as ISSYSTEM_XS,"+
                "TYPE,DECODE(TYPE,'0','库区','1','停车场') as TYPE_XS from T_SYS_ORG order by ORGCODE";
            DataTable dt = DataAccess.ExecuteDataTable(strSQL);
            this.cdgvKQ.AutoGenerateColumns = false;
            this.cdgvKQ.DataSource = dt;
        }

        protected override void OnNew()
        {
            base.OnNew();

            using (KQGLForm form = new KQGLForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("新增库区成功！");
                    OnLoadModule();
                }
            }
        }

        protected override void OnModify()
        {
            base.OnModify();

            if (cdgvKQ.SelectedCells != null && cdgvKQ.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvKQ.SelectedCells[0].OwningRow;
                OrgInfo _org = new OrgInfo();
                _org.ID = row.Cells["ID"].Value.ToString();
                _org.OrgCode = row.Cells["OrgCode"].Value.ToString();
                _org.OrgName = row.Cells["OrgName"].Value.ToString();
                _org.OrgType = Convert.ToInt32(row.Cells["Type"].Value.ToString());
                using (KQGLForm form = new KQGLForm(_org))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("修改库区信息成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        protected override void OnDelete()
        {
            base.OnDelete();
            if (cdgvKQ.SelectedCells != null && cdgvKQ.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvKQ.SelectedCells[0].OwningRow;
                if (row.Cells["ISSYSTEM"].Value.ToString() == "1")
                {
                    MessageBox.Show("系统内置库区不能删除！");
                    return;
                }
                if (MessageBox.Show("确定要删除选中库区吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string strID = row.Cells["ID"].Value.ToString();
                    int i = DataAccess.ExecuteNonQuery("delete from T_SYS_ORG where ID='" + strID + "'");
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        private void cdgvKQ_SelectionChanged(object sender, EventArgs e)
        {
            if (cdgvKQ.SelectedCells != null && cdgvKQ.SelectedCells.Count > 0)
            {
                string strCode = cdgvKQ.SelectedCells[0].OwningRow.Cells["ORGCODE"].Value.ToString();
                if (strCode != selectedOrgCode)
                {
                    selectedOrgCode = strCode;
                    LoadUserData();
                }
            }
            else
            {
                cdgvYH.DataSource = null;
                selectedOrgCode = string.Empty;
            }
        }

        private void LoadUserData()
        {
            if (!string.IsNullOrEmpty(selectedOrgCode))
            {
                string strSql = "select ID,USERNAME,PASSWORD,ROLE,ISSYSTEM,'管理员' as ROLE_XS,XM,'是' as ISSYSTEM_XS " +
                    "from T_SYS_USER where ISSYSTEM = '1' and ORGCODE = '" + selectedOrgCode + "' ORDER BY USERNAME";
                DataTable dtYHGL = DataAccess.ExecuteDataTable(strSql);
                cdgvYH.AutoGenerateColumns = false;
                cdgvYH.DataSource = dtYHGL;
            }
        }

        private void tsBtnAddUser_Click(object sender, EventArgs e)
        {
            if (cdgvKQ.SelectedCells != null && cdgvKQ.SelectedCells.Count > 0)
            {
                string strCode = cdgvKQ.SelectedCells[0].OwningRow.Cells["ORGCODE"].Value.ToString();
                using (YHGLForm form = new YHGLForm(true, strCode))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("新增用户成功！");
                        LoadUserData();
                    }
                }
            }
        }

        private void tsBtnModifyUser_Click(object sender, EventArgs e)
        {
            if (cdgvYH.SelectedCells != null && cdgvYH.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvYH.SelectedCells[0].OwningRow;
                UserInfo _user = new UserInfo();
                _user.ID = row.Cells["U_ID"].Value.ToString();
                _user.UserName = row.Cells["USERNAME"].Value.ToString();
                _user.Password = row.Cells["PASSWORD"].Value.ToString();
                _user.XM = row.Cells["XM"].Value.ToString();
                _user.Role = row.Cells["U_ROLE"].Value.ToString();
                _user.IsSystem = row.Cells["U_ISSYSTEM"].Value.ToString();
                using (YHGLForm form = new YHGLForm(_user))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("修改用户信息成功！");
                        LoadUserData();
                    }
                }
            }
        }

        private void tsDeleteUser_Click(object sender, EventArgs e)
        {
            if (cdgvYH.SelectedCells != null && cdgvYH.SelectedCells.Count > 0)
            {
                if (MessageBox.Show("确定要删除选中用户吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string strID = cdgvYH.SelectedCells[0].OwningRow.Cells["U_ID"].Value.ToString();
                    int i = DataAccess.ExecuteNonQuery("delete from T_SYS_USER where ID = '" + strID + "'");
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功！");
                        LoadUserData();
                    }
                }
            }
        }
    }
}
